<template>
  <div class="ccui-app-container contract-approval">
    <div class="ccui-advanced-search-wrapper ccui-clearfix">
      <el-search
        :formProp="formData"
        @search="search"
        :isShowSearch="isShowSearch"
        class="ccui-highsearch"
        :selectClear="true"
        :isShow="isShow"
      >
        <div class="ccui-tab-wrapper">
          <el-button
            :type="isActive === index ? 'table-top-active' : 'table-top'"
            size="mini"
            v-for="(item, index) in focuses"
            :key="index"
            @click="tabChange(item.focus)"
          >
            <template v-if="item.focus === 'All'">全部</template>
            <template v-if="item.focus === 'Submit'"
              >待提交({{ totalNum || 0 }})</template
            >
            <template v-else-if="item.focus === 'Approval'">审批中</template>
            <template v-else-if="item.focus === 'ApprovalTurn'"
              >审批退回</template
            >
          </el-button>
        </div>
        <template slot="search">
          <el-input
            v-model="formData.replacePayName"
            clearable
            searchLabel="代付单名称"
            placeholder="请输入代付单名称模糊查询"
            slot="searchItem2"
            @blur="handleTrim('replacePayName')"
          />
          <el-input
            v-model="formData.replacePayCode"
            clearable
            searchLabel="代付单编号"
            placeholder="请输入代付单编号模糊查询"
            slot="searchItem1"
            @blur="handleTrim('replacePayCode')"
          />
          <el-input
            v-model="formData.contractCode"
            clearable
            searchLabel="合同编号"
            placeholder="请输入合同编号模糊查询"
            slot="searchItem3"
            @blur="handleTrim('contractCode')"
          />
          <el-input
            v-model="formData.contractName"
            clearable
            searchLabel="合同名称"
            placeholder="请输入合同名称模糊查询"
            slot="searchItem5"
            @blur="handleTrim('contractName')"
          />
          <el-input
            v-model="formData.needName"
            clearable
            searchLabel="需方名称"
            placeholder="请输入需方名称模糊查询"
            slot="searchItem6"
            @blur="handleTrim('needName')"
          />
          <el-input
            v-model="formData.contacts"
            clearable
            searchLabel="联系人"
            placeholder="请输入联系人模糊查询"
            slot="searchItem8"
            @blur="handleTrim('contacts')"
          />
          <el-input
            v-model="formData.supplierName"
            clearable
            searchLabel="供方名称"
            placeholder="请输入供方名称模糊查询"
            slot="searchItem7"
            @blur="handleTrim('supplierName')"
          />
        </template>
        <el-input
          v-model="formData.replacePayName"
          clearable
          searchLabel="代付单名称"
          placeholder="请输入代付单名称模糊查询"
          slot="searchItem2"
          @blur="handleTrim('replacePayName')"
        />
        <el-input
          v-model="formData.replacePayCode"
          clearable
          searchLabel="代付单编号"
          placeholder="请输入代付单编号模糊查询"
          slot="searchItem1"
          @blur="handleTrim('replacePayCode')"
        />
        <el-input
          v-model="formData.contractCode"
          clearable
          searchLabel="合同编号"
          placeholder="请输入合同编号模糊查询"
          slot="searchItem3"
          @blur="handleTrim('contractCode')"
        />
        <el-input
          v-model="formData.contractName"
          clearable
          searchLabel="合同名称"
          placeholder="请输入合同名称模糊查询"
          slot="searchItem5"
          @blur="handleTrim('contractName')"
        />
        <el-input
          v-model="formData.needName"
          clearable
          searchLabel="需方名称"
          placeholder="请输入需方名称模糊查询"
          slot="searchItem6"
          @blur="handleTrim('needName')"
        />
        <el-input
          v-model="formData.contacts"
          clearable
          searchLabel="联系人"
          placeholder="请输入联系人模糊查询"
          slot="searchItem8"
          @blur="handleTrim('contacts')"
        />
        <el-input
          v-model="formData.supplierName"
          clearable
          searchLabel="供方名称"
          placeholder="请输入供方名称模糊查询"
          slot="searchItem7"
          @blur="handleTrim('supplierName')"
        />
      </el-search>
    </div>
    <div class="ccui-multifunctional-table-wrapper">
      <el-table
        v-if="tableHack"
        ref="multipleTable"
        :data="tableData"
        :fit="true"
        :row-style="rowClass"
        :cell-style="cellStyle"
        @cell-click="cellClick"
        border
        class="ccui-multifunctional-table"
        @selection-change="handleSelectionChange"
        @header-dragend="tableTitleWidth"
        :min-height="9999"
      >
        <template v-for="(table, index) in tableTheadOptions">
          <el-table-column
            v-if="table.checked"
            :fixed="table.fixed || false"
            :type="table.type"
            :key="index"
            :label="table.label"
            :min-width="table.width"
            :prop="table.prop"
            :filters="table.filters"
            :align="table.align"
            :header-align="table.headerAlign || 'left'"
            :show-overflow-tooltip="true"
          ></el-table-column>
        </template>

        <el-table-column label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleAudit(scope.row)"
              >审核</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="ccui-table-footer" v-if="totalNum > 0">
        <div id="ccui-paging">
          <el-pagination
            :total="totalNum"
            :page-size="getLocalPageSize()"
            :current-page="formDataSearch.pageNo"
            :page-sizes="[10, 20, 50, 100, 200, 500]"
            background
            layout="total, sizes, prev, pager, next, jumper"
            popper-class="ccui-paging-page-size-popper"
            @prev-click="handlePrev"
            @next-click="handleNext"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
require("core-js/modules/es.object.assign");
require("core-js/modules/es.object.keys");
require("regenerator-runtime/runtime");
require("core-js/modules/es.object.values");
require("core-js/modules/es.object.set-prototype-of");
require("core-js/modules/es.array.find");
require("core-js/modules/es.array-buffer.is-view");
require("core-js/modules/es.weak-map");
require("core-js/modules/es.promise");
import { getDataList } from "../api";
import { setSessionItem, getSessionItem, ishasAgentId } from "@/basa/utils";
export default {
  name: "paidConfirmSubmit",
  data() {
    return {
      //多页签
      focuses: [
        { focus: "All" },
        { focus: "Submit" },
        { focus: "Approval" },
        { focus: "ApprovalTurn" }
      ],
      isActive: 1,
      // 表格数据
      tableData: [],
      tableHack: true,
      // 表格命名
      tableName: this.tableList.paidConfirmSubmit,
      // 表格数据
      tableThead: [],
      // 锁定表格
      fixedTableThead: [
        {
          label: "序号",
          prop: "id",
          width: "38",
          checked: true,
          fixed: true,
          type: "index"
        },
        {
          label: "代付单编号",
          prop: "replacePayCode",
          width: "150",
          checked: true,
          fixed: true,
          align: "left"
        },
        {
          label: "合同编号",
          prop: "contractCode",
          width: "150",
          checked: true,
          fixed: true,
          align: "left"
        }
      ],
      // 非锁定表格
      unfixedTableThead: [
        {
          label: "代付单名称",
          prop: "replacePayName",
          width: "200",
          checked: true,
          align: "left"
        },
        {
          label: "合同名称",
          prop: "contractName",
          width: "200",
          checked: true,
          align: "left"
        },
        {
          label: "需方名称",
          prop: "needName",
          width: "200",
          checked: true,
          align: "left"
        },
        {
          label: "付款金额(元)",
          prop: "paymentMoney",
          width: "100",
          checked: true,
          align: "right"
        },
        {
          label: "供方名称",
          prop: "supplierName",
          width: "200",
          checked: true,
          align: "left"
        },
        {
          label: "联系人",
          prop: "contacts",
          width: "90",
          checked: true,
          align: "left"
        },
        {
          label: "编制日期",
          prop: "reportTime",
          width: "100",
          checked: true,
          align: "left"
        },
        {
          label: "状态",
          prop: "replacePayStatusName",
          width: "100",
          checked: true,
          align: "left"
        }
      ],
      //全部数量
      totalNum: 0,
      // 动态表格
      tableTheadOptions: [],
      // 弹框表格
      popoverShow: false,
      selectRow: [],
      selectData: [],
      // 高级搜索数据 start
      // 高级搜索提交数据
      isShowSearch: true,
      isShow: true,
      formData: {
        replacePayStatus: 1,
        replacePayCode: "",
        replacePayName: "",
        contractCode: "",
        supplierName: "",
        contractName: "",
        needName: "",
        contacts: "",
        reportOrgId: getSessionItem("userInfo", "orgId"),
        pageNo: 1,
        pageSize: this.getLocalPageSize(),
        isShowPageLoading: true
      },
      formDataSearch: {
        replacePayStatus: 1,
        replacePayCode: "",
        replacePayName: "",
        contractCode: "",
        supplierName: "",
        contractName: "",
        needName: "",
        contacts: "",
        reportOrgId: getSessionItem("userInfo", "orgId"),
        pageNo: 1,
        pageSize: this.getLocalPageSize(),
        isShowPageLoading: true
      }
      // 高级搜索数据 end
    };
  },
  activated() {
    this.getDataList(this.formData);
  },
  mounted() {
    this.initData();
    this.getDataList(this.formData);
  },
  methods: {
    //去空格
    handleTrim(key) {
      if (this.formData[key]) {
        this.$set(
          this.formData,
          key,
          this.formData[key].replace(/(^\s*)|(\s*)$/g, "")
        );
      }
    },
    initData() {
      this.setTableWidth();
      this.renderTable();
    },
    getDataList(data) {
      getDataList(data).then(res => {
        if (res.data.code == 0) {
          this.tableData = res.data.data.rows;
          this.tableData.forEach(item => {
            item.replacePayStatusName =
              item.replacePayStatus == 1
                ? "待提交"
                : item.replacePayStatus == 2
                ? "审批中"
                : item.replacePayStatus == 3
                ? "审批退回"
                : item.replacePayStatus == 4
                ? "审批通过"
                : "";
            if (item.paymentMoney || item.paymentMoney === 0) {
              item.paymentMoney = Number(item.paymentMoney).toFixed(2);
            }
            if (item.reportTime) {
              item.reportTime = item.reportTime.substring(0, 10);
            }
          });
          this.totalNum = res.data.data.recordsTotal;
        }
      });
    },
    // 页签跳转
    tabChange(data) {
      this.$router.push({
        path: "/paymentManagement/paidConfirmList/" + `paidConfirm${data}`
      });
    },
    //审核
    handleAudit(row) {
      this.$router.push({
        path: "/paymentManagement/paidConfirmList/paidConfirmDetails",
        query: {
          id: row.id,
          ifButton: 1
        }
      });
    },
    // 查看
    cellClick(row, column, columnIndex) {
      if (column.label === "代付单编号") {
        this.$router.push({
          path: "/paymentManagement/paidConfirmList/paidConfirmDetails",
          query: {
            id: row.id
          }
        });
      }
      if (column.label === "合同编号") {
        if (row.contractType == 1) {
          this.$router.push({
            path: "/contractManagement/contractAgreement/item-detail",
            query: {
              concId: row.contractId,
              ifApprovingStatus: 0,
              parameterList: 1,
              type: "account"
            }
          });
        } else {
          this.$router.push({
            path: "/contractManagement/transportContractAgreement/item-detail",
            query: {
              concId: row.contractId,
              ifApprovingStatus: 0,
              parameterList: 1,
              type: "account"
            }
          });
        }
      }
    },
    // 根据返回状态显示不同的文字颜色
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { "background-color": "transparent" };
      }
      if (columnIndex == 1 || columnIndex === 2) {
        return { color: "#0089EF", cursor: "pointer" };
      }
    },
    // 默认渲染表格
    renderTable() {
      const localTableData = localStorage.getItem(this.tableName)
        ? JSON.parse(localStorage.getItem(this.tableName))
        : [];
      let unfixedTableThead = this.fixedTableThead.concat(
        this.unfixedTableThead
      );
      let localTableDataStr = localTableData.map(
        ({ label, prop, formatter, align }) =>
          JSON.stringify({ label, prop, formatter, align })
      );
      let unfixedTableTheadStr = unfixedTableThead.map(
        ({ label, prop, formatter, align }) =>
          JSON.stringify({ label, prop, formatter, align })
      );

      if (localTableData.length === unfixedTableThead.length) {
        let flag = localTableDataStr.every(item =>
          unfixedTableTheadStr.includes(item)
        );
        if (flag) {
          // 完全相同
          this.tableTheadOptions = localTableData;
          this.tableThead = localTableData;
          localTableData.forEach(item => {
            let index = this.unfixedTableThead.findIndex(
              cItem => item.prop === cItem.prop
            );
            index !== -1 &&
              (this.unfixedTableThead[index].checked = item.checked);
          });
        } else {
          // 长度相同但值不同
          this.tableThead = this.fixedTableThead.concat(this.unfixedTableThead);
          const comment = JSON.stringify(this.tableThead);
          localStorage.setItem(this.tableName, comment);
          const localTableData = JSON.parse(
            localStorage.getItem(this.tableName)
          );
          this.tableTheadOptions = localTableData;
        }
      } else {
        // 长度不同
        this.tableThead = this.fixedTableThead.concat(this.unfixedTableThead);
        const comment = JSON.stringify(this.tableThead);
        localStorage.setItem(this.tableName, comment);
        const localTableData = JSON.parse(localStorage.getItem(this.tableName));
        this.tableTheadOptions = localTableData;
      }
    },
    // 渲染本地存储表格宽度
    setTableWidth() {
      const localTableData = JSON.parse(localStorage.getItem(this.tableName));
      if (localTableData) {
        for (let i = 0; i < this.tableTheadOptions.length; i++) {
          if (this.tableTheadOptions[i].label === localTableData[i].label) {
            this.tableTheadOptions[i].width = localTableData[i].width;
          }
        }
      }
    },
    // 表格宽度更改存入
    tableTitleWidth(newWidth, oldWidth, column, event) {
      for (let i = 0; i < this.unfixedTableThead.length; i++) {
        if (this.unfixedTableThead[i].label === column.label) {
          this.unfixedTableThead[i].width = newWidth;
        }
      }
      this.tableThead = this.unfixedTableThead.concat(this.fixedTableThead);
      const comment = JSON.stringify(this.tableThead);
      localStorage.setItem(this.tableName, comment);
      this.tableTheadOptions = JSON.parse(localStorage.getItem(this.tableName));
      this.tableHack = false;
      this.$nextTick(() => {
        this.tableHack = true;
      });
    },
    handleSelectionChange(data) {
      this.selectData = data;
    },
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { "background-color": "rgba(255,232,195,1) " };
      }
    },
    // 分页
    // 上一页
    handlePrev() {},
    // 下一页
    handleNext() {},
    //  显示条数更改
    handleSizeChange(val) {
      this.formDataSearch.pageNo = 1;
      this.formDataSearch.pageSize = val;
      setSessionItem("pageSize", this.tableName, val);
      this.getDataList(this.formDataSearch);
    },
    // 获取默认条数
    getLocalPageSize() {
      return getSessionItem("pageSize", this.tableList.paidConfirmSubmit) || 10;
    },
    // 当前页码更改
    handleCurrentChange(val) {
      // 跳转翻页都会调这个方法
      this.formDataSearch.pageNo = val;
      this.getDataList(this.formDataSearch);
    },
    // 分页 end
    // 高级搜索所需函数 start
    // 高级搜索展开按钮
    search(searchData) {
      this.formDataSearch = JSON.parse(JSON.stringify(searchData));
      this.formDataSearch.reportOrgId = getSessionItem("userInfo", "orgId");
      this.formDataSearch.pageNo = 1;
      this.formDataSearch.isShowPageLoading = true;
      this.formDataSearch.replacePayStatus = 1;
      this.formDataSearch.pageSize = this.getLocalPageSize();
      this.getDataList(this.formDataSearch);
    }
    // 高级搜索 end
  },
  beforeRouteLeave(to, from, next) {
    for (var key in this.$vnode.parent.parent.componentInstance.cache) {
      delete this.$vnode.parent.parent.componentInstance.cache[
        ishasAgentId(to.path, key)
      ];
    }
    next();
  }
};
</script>
<style lang="scss" scoped>
/deep/ #ccui-paging .ccui-pagination-btn {
  display: block;
}
.contract-approval {
  /deep/ .el-searchs {
    display: none;
  }
  /deep/ .el-search .icon-ccui-downdown {
    display: none;
  }
}
</style>
